package com.mc.web;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;


import com.mc.model.Department;
import com.mc.model.IndexImage;
import com.mc.model.Lab;
import com.mc.model.Major;
import com.mc.model.Path;
import com.mc.model.ResearchLab;
import com.mc.model.ScienceDirection;
import com.mc.model.ScienceDirectionWithBLOBs;
import com.mc.model.ScienceTeacher;
import com.mc.model.SubMenu;
import com.mc.model.Teacher;
import com.mc.model.Teteacherytpe;
import com.mc.model.Tmenu;
import com.mc.model.User;
import com.mc.model.Xxydtype;
import com.mc.model.Xy;
import com.mc.model.Xyyd;
import com.mc.model.XyydType;
import com.mc.model.newsandbulletin;
import com.mc.service.IDepartmentService;
import com.mc.service.IIndexImageService;
import com.mc.service.ILabService;
import com.mc.service.IResearchLabService;
import com.mc.service.IScienceDirectionService;
import com.mc.service.IScienceTeacherService;
import com.mc.service.IXXydService;
import com.mc.service.IXxydtypeService;
import com.mc.service.MajorService;
import com.mc.service.MenuService;
import com.mc.service.SubMenuService;
import com.mc.service.TeacherService;
import com.mc.service.TeteachertypeService;
import com.mc.service.TmenuService;
import com.mc.service.UserService;
import com.mc.serviceImpl.XxydServiceImpl;
import com.mc.utils.PathUtil;
import com.mc.utils.StringUtil;
import com.mc.utils.StringUtil2;
import com.mc.utils.TraderCenterUtil;

@Controller
@RequestMapping("/backend")
@SessionAttributes("user")
public class BackendAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(BackendAction.class);

	@Autowired
	private UserService userService;
	
	@Autowired private MenuService menuService;
	@Autowired private SubMenuService subMenuService;
	@Autowired private TmenuService tmenuService;
	
	@Autowired private TeteachertypeService teteachertypeService;
	
	@Autowired private IResearchLabService labService;
	
	@Autowired private MajorService majorService;
	
	@Autowired private TeacherService teacherService;
	
	@Autowired private IScienceDirectionService directionService;
	@Autowired private IScienceTeacherService scienceTeacherService;
	
	@Autowired private ILabService iLabService;
	@Autowired private IDepartmentService departmentService;
	
    @Autowired private IXxydtypeService xxydtypeService;
	
	@Autowired private IXXydService xydService;
	
	@Autowired private IIndexImageService indexImageService;
	private Path path = null;
	
	/**获取二级菜单*/
	@RequestMapping("/getsubmenus")
	@ResponseBody
	public String getSubMenus(@RequestParam(value="pmid",required=true)int pmid,ModelMap map)
	{
		
		List<SubMenu> smenus = subMenuService.selectSubMenuByMid(pmid);
		String smenujson = TraderCenterUtil.getInstance().ParseListToJSON(smenus);
		//logger.info(smenujson + "----");
		return smenujson;
	}
	
	/**获取三级菜单*/
	@RequestMapping("/gettmenus")
	@ResponseBody
	public String getTmenus(@RequestParam("sid")int sid)
	{
		//logger.info(sid + "----sid");
		List<Tmenu> tmenus = tmenuService.selectTmenuBysid(sid);
		String tmenujson = TraderCenterUtil.getInstance().ParseListToJSON(tmenus);
		return tmenujson;
	}
	
	@RequestMapping("/AcaIntro")
	public String setAcaIntro(@RequestParam(value="content",required = true) String content)
	{
		
		return "";
	}
	@RequestMapping("/adddata")
	public String addData(@RequestParam(value="pmenu",required=true)int pmid,
			@RequestParam(value="smenu",required=true) int sid,
			@RequestParam(value="tmenu",required=true) int tid,
			@RequestParam(value="context",required=true) String context)
	{
		//logger.info(pmid +" "+ sid + " " + tid + " \n" + context);
		
		return "";
	}
	/**
	 * 
	 * @param modelMap
	 * @return  返回后台添加研究所的页面
	 */
	@RequestMapping("/addResearchLabui")
	public String addResearchLabui(ModelMap modelMap){
		
		//List<Teteacherytpe> ts = teteachertypeService.selectAllsteacher();
		//modelMap.addAttribute("ts",ts);
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addResearchLab","ViewTitle:添加研究所","smenu:researchlab","mmenu:addlab"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param content  研究所的简介
	 * @param lab 研究所的信息类对象
	 * @param modelMap 
	 * @return 成功添加的时候返回研究所的管理页面
	 */
	@RequestMapping("/addResearchLab")
	public String addResearchLab(@RequestParam("content")String content,ResearchLab lab,ModelMap modelMap ){
		
		
		lab.setResearchlabIntro(content);
		int result = labService.insert(lab);
		
		return "forward:/backend/rlabmgui";
	}
	/**
	 * 
	 * @param modelMap
	 * @return  返回添加专业的页面
	 */
	@RequestMapping("/addzyui")
	public String addzyui(ModelMap modelMap){
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addzy","ViewTitle:添加专业","smenu:bmajor","mmenu:addmajor"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param record  有关专业的信息
	 * @return 如果添加成功就返回专业管理的页面
	 */ 
	@RequestMapping("/addzy")
	public String addzy(ModelMap modelMap,Major record ){
		
		
		int result = majorService.insert(record);
		if(result!=0){
			modelMap.addAttribute("message", true);
		}
	//	logger.info(result + "----result");
		return "forward:/backend/zymgui";
	}
	/**
	 * 
	 * @param modelMap
	 * @return 返回专业管理的页面
	 */
	@RequestMapping("/zymgui")
	public String zymgui(ModelMap modelMap){
		List<Major> majors = majorService.selectAllMajors();
		modelMap.addAttribute("majors",majors);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:zymag","ViewTitle:添加专业","smenu:bmajor","mmenu:mgmajor"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param id  需要更新问专业id
	 * @param modelMap
	 * @return 返回更新专业信息的页面
	 */
	@RequestMapping("/updatezy/{id}")
	public String updatezy(@PathVariable("id")int id,ModelMap modelMap){
		Major major = majorService.selectByPrimaryKey(id);
		if(major.getIntroduce()!=null && !major.getIntroduce().equals(""))
			major.setIntroduce(StringUtil.getInstace().zy(major.getIntroduce()));
		if(major.getMajorPlan()!=null && !major.getMajorPlan().equals("")){
			major.setMajorPlan(StringUtil.getInstace().zy(major.getMajorPlan()));
		}
		modelMap.addAttribute("major",major);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatezy","ViewTitle:添加专业","smenu:bmajor","mmenu:updatemajor"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param major 前台传输过来的有关专业信息的类对象
	 * @return  如果修改专业信息成功，则返回专业管理的页面
	 */
	@RequestMapping("/updatezyxx")
	public String updatezyxx(ModelMap modelMap,Major major){
		
		//logger.info(major.toString());
		int result = majorService.updateByPrimaryKeyWithBLOBs(major);
		if(result!=0){
			modelMap.addAttribute("update", true);
		}
		//logger.info(result);
		return "forward:/backend/zymgui";
	}
	/**
	 * 
	 * @param id  需要进行ajax删除的专业的id
	 * @param modelMap
	 * @param request 用于获取跨域ajax需要返回的js函数的名称
	 * @return  ajax返回元页面，进行异步的刷新
	 */ 
	@RequestMapping("/delmajor")
	@ResponseBody
	 public String delmajor(@RequestParam("id") int id,ModelMap modelMap,HttpServletRequest request)
	 {
		 // logger.info(id);
		 int resultCode = majorService.deleteByPrimaryKey(id);
		// System.out.println(resultCode);
		 String callbackFunName = request.getParameter("callbackparam");
		 if(resultCode!=0)
		 {
			// modelMap.addAttribute("delFile", true);	 
			 return callbackFunName +"("+ "{'success':true}" +")";
		 }else{
			 //modelMap.addAttribute("delFile", false);
			 return callbackFunName +"("+ "{'success':false}" +")";
		 }
		 
		// return "forward:/backend/ZiliaoManeger/1";
	 }
	/**
	 * 
	 * @param modelMap
	 * @return 返回实验室的管理页面
	 */
	@RequestMapping("/rlabmgui")
	public String rlabmg(ModelMap modelMap){
		
		List<ResearchLab> labs = labService.selectAllResearchLab();
		modelMap.addAttribute("labs", labs);
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:researchlabmg","ViewTitle:添加专业","smenu:researchlab","mmenu:mglab"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param id  需要进行ajax删除的实验室id
	 * @param modelMap
	 * @param request 用于获取跨域ajax需要返回的js函数的名称
	 * @return  ajax返回元页面，进行异步的刷新
	 */ 
	@RequestMapping("/dellab")
	@ResponseBody
	 public String dellab(@RequestParam("id") int id,ModelMap modelMap,HttpServletRequest request
		)
	 {
		 // logger.info(id);
		 int resultCode = labService.deleteByPrimaryKey(id);
		 String callbackFunName = request.getParameter("callbackparam");
		 if(resultCode!=0)
		 {
			// modelMap.addAttribute("delFile", true);	 
			 return callbackFunName +"("+ "{'success':true}" +")";
		 }else{
			 //modelMap.addAttribute("delFile", false);
			 return callbackFunName +"("+ "{'success':false}" +")";
		 }
		 
		// return "forward:/backend/ZiliaoManeger/1";
	 }
	/**
	 * 
	 * @param id  需要修改信息的实验室id
	 * @param modelMap
	 * @return 返回需要更新的实验室页面
	 */
	@RequestMapping("/updatelab/{id}")
	public String updatelab(@PathVariable("id")int id,ModelMap modelMap){
		ResearchLab lab = labService.selectByPrimaryKey(id);
		if(lab.getResearchlabIntro()!=null && !lab.getResearchlabIntro().equals(""))
		
		   lab.setResearchlabIntro(StringUtil.getInstace().zy(lab.getResearchlabIntro()));
		modelMap.addAttribute("lab",lab);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatelab","ViewTitle:修改研究所信息","smenu:researchlab","mmenu:updatelab"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param lab  前台返回的实验室的信息类对象
	 * @return 如果修改实验室信息成功，则返回实验室的管理页面
	 */
	@RequestMapping("/updatelabxx")
	public String updatelabxx(ModelMap modelMap,ResearchLab lab){
		
		//logger.info(lab.toString());
		int result = labService.updateByPrimaryKeyWithBLOBs(lab);
		if(result!=0){
			modelMap.addAttribute("update", true);
		}
		//logger.info(result);
		return "forward:/backend/rlabmgui";
	}
	/**
	 * 
	 * @param modelMap
	 * @return 返回科研方向（科研团队）的添加界面
	 */
	@RequestMapping("/addScienceDirectionui")
	public String addScienceDirectionui(ModelMap modelMap){
		
		List<Teacher> teachers = teacherService.selectAllTeacher();
		modelMap.addAttribute("teachers",teachers);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addScienceDirection","ViewTitle:添加科学研究方向","smenu:sciencedirection","mmenu:adddirection"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param ids1 科研方向的带头人id
	 * @param ids 科研方向的成员id
	 * @param modelMap
	 * @param directionWithBLOBs  科研方向的信息类对象
	 * @param session content上下文session
	 * @return  返回管理科研方向的界面
	 */
	@RequestMapping("/addScienceDirection")
	public String addScienceDirection(@RequestParam("ids1")String ids1,
			@RequestParam("ids")String ids,ModelMap modelMap,
			ScienceDirectionWithBLOBs directionWithBLOBs,HttpSession session){
		
		//logger.info(directionWithBLOBs.toString());
		int result1 = 0;
		Map<String, Object> params = this.setParameters(directionWithBLOBs,
				((User)session.getAttribute("user")).getId(), result1);
	//	System.out.println(directionWithBLOBs.toString());
		directionService.insert(params);
		
		String [] id = ids.split(",");
		for(String id1:id)
		{
			if(!id1.equals(""))
			{
				Teacher teacher = teacherService.selectByPrimaryKey(Integer.parseInt(id1.toString().trim()));
				ScienceDirection direction = directionService.selectByPrimaryKey((Integer)params.get("result"));
				ScienceTeacher record = new ScienceTeacher();
				record.settId(teacher);
				record.setsId(direction);
				record.setIsindex(true);
				scienceTeacherService.insert(record );
			}
		}
		String [] id1 = ids1.split(",");
		for(String id2:id1)
		{
			if(!id2.equals(""))
			{
			//	logger.info(id2 + "----");
				Teacher teacher = teacherService.selectByPrimaryKey(Integer.parseInt(id2.toString().trim()));
				ScienceDirection direction = directionService.selectByPrimaryKey((Integer)params.get("result"));
				ScienceTeacher record = new ScienceTeacher();
				record.settId(teacher);
				record.setsId(direction);
				record.setIsindex(false);
				scienceTeacherService.insert(record );
			}
		}
						
		return "forward:/backend/sciencedirectionmgui";
	}
	/**
	 * 
	 * @param modelMap
	 * @return 返回科研方向的管理界面
	 */
	@RequestMapping("/sciencedirectionmgui")
	public String sciencedirectionmgui(ModelMap modelMap){
		
		List<ScienceDirection> directions = directionService.selectalldirections();
		modelMap.addAttribute("directions", directions);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:scienceDirectionmag","ViewTitle:管理科学研究方向信息","smenu:sciencedirection","mmenu:mgdirection"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param bs 科研方向的类对象
	 * @param userid 管理员的id
	 * @param result 操作数据库返回的参数
	 * @return  设置操作数据库的参数列表 
	 */
	public Map<String,Object> setParameters(ScienceDirectionWithBLOBs bs,int userid,int result){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("direction_name1", bs.getDirectionName());
		params.put("direction_intro1", bs.getDirectionIntro());
		params.put("direction_achievement1", bs.getDirectionAchievement());
		params.put("userid", userid);
		params.put("result", result);
		return params;
	}
	/**
	 * 
	 * @param modelMap
	 * @param id 需要更新的科研方向id
	 * @return 返回需要更新的科研方向界面
	 */
	@RequestMapping("/updatedirectionui/{id}")
	public String updatedirectionui(ModelMap modelMap,@PathVariable("id")int id){
		
		List<Teacher> teachers = teacherService.selectAllTeacher();
		modelMap.addAttribute("teachers",teachers);
		ScienceDirectionWithBLOBs directionWithBLOBs = directionService.selectByPrimaryKey(id);
		List<ScienceTeacher> sciencefzTeacher = scienceTeacherService.selectbydirectionfzid(id);
		modelMap.addAttribute("leaders", sciencefzTeacher);
		if(directionWithBLOBs.getDirectionAchievement()!=null && !directionWithBLOBs.getDirectionAchievement().equals(""))
			directionWithBLOBs.setDirectionAchievement(StringUtil.getInstace().zy(directionWithBLOBs.getDirectionAchievement()));
		if(directionWithBLOBs.getDirectionIntro()!=null && !directionWithBLOBs.getDirectionIntro().equals(""))
			directionWithBLOBs.setDirectionIntro(StringUtil.getInstace().zy(directionWithBLOBs.getDirectionIntro()));
		List<ScienceTeacher> scienceTeacher = scienceTeacherService.selectbydirectionid(id);
		modelMap.addAttribute("members", scienceTeacher);
		modelMap.addAttribute("direction", directionWithBLOBs);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatedirection","ViewTitle:修改科学研究方向信息","smenu:sciencedirection","mmenu:updatedirection"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param directions  前台传入的科研方向对象实例
	 * @param ids1 带头人id
	 * @param ids 团队成员id
	 * @return 返回管理科研方向的界面
	 */
	@RequestMapping("/updateScienceDirection")
	public String updateScienceDirection(ModelMap modelMap,
			ScienceDirectionWithBLOBs directions,@RequestParam("ids1")String ids1,
			@RequestParam("ids")String ids){
		//logger.info(ids+"----------" + ids1); 
		int result =scienceTeacherService.deleteByPrimaryKey(directions.getDirectionId());
		//logger.info(directions.toString());
		directionService.updateByPrimaryKeyWithBLOBs(directions);
		String [] id = ids.split(",");
		for(String id1:id)
		{
			if(!id1.equals(""))
			{
				Teacher teacher = teacherService.selectByPrimaryKey(Integer.parseInt(id1.toString().trim()));
				ScienceTeacher record = new ScienceTeacher();
				record.settId(teacher);
				record.setsId(directions);
				record.setIsindex(true);
				scienceTeacherService.insert(record );
			}
		}
		String [] id1 = ids1.split(",");
		for(String id2:id1)
		{
			if(!id2.equals(""))
			{
			//	logger.info(id2 + "----");
				Teacher teacher = teacherService.selectByPrimaryKey(Integer.parseInt(id2.toString().trim()));
				ScienceTeacher record = new ScienceTeacher();
				record.settId(teacher);
				record.setsId(directions);
				record.setIsindex(false);
				scienceTeacherService.insert(record );
			}
		}
		
		return "forward:/backend/sciencedirectionmgui";
	}
	/**
	 * 
	 * @param id  需要进行ajax删除的科研方向的id
	 * @param modelMap
	 * @param request 用于获取跨域ajax需要返回的js函数的名称
	 * @return  ajax返回元页面，进行异步的刷新
	 */ 
	@RequestMapping("/deldirection")
	@ResponseBody
	 public String deldirection(@RequestParam("id") int id,ModelMap modelMap,
			 HttpSession session,HttpServletRequest request)
	 {
		 directionService.deldirection(id,((User)session.getAttribute("user")).getId());
		 String callbackFunName = request.getParameter("callbackparam");
		
			// modelMap.addAttribute("delFile", true);	 
			 return callbackFunName +"("+ "{'success':true}" +")";
		
	 }
	
	/**
	 * 
	 * @param modelMap
	 * @return 返回用于添加实验室的界面
	 */
	@RequestMapping("/addlabui")
	public String addlabui(ModelMap modelMap){
		
		//List<Teteacherytpe> ts = teteachertypeService.selectAllsteacher();
		//modelMap.addAttribute("ts",ts);
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addlab","ViewTitle:添加实验室","smenu:lab","mmenu:addlab"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param lab 前台传入的管理员输入的实验室信息
	 * @param modelMap
	 * @return 成功的话，则返回管理实验室的界面
	 */
	@RequestMapping("/addlab")
	public String addlab(Lab lab,ModelMap modelMap ){
		
		iLabService.insert(lab);
		return "forward:/backend/labmgui";
	}
	/**
	 * 
	 * @param modelMap
	 * @return 返回用于管理实验室的界面
	 */
	@RequestMapping("/labmgui")
	public String labmg(ModelMap modelMap){
		
		List<Lab> labs = iLabService.selectalllab();
		modelMap.addAttribute("labs", labs);
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:labmg","ViewTitle:实验室管理","smenu:lab","mmenu:mglab"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 修改实验室信息
	 * @param id 实验室id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/updatexlab/{id}")
	public String updatexlab(@PathVariable("id")int id,ModelMap modelMap){
		Lab lab = iLabService.selectByPrimaryKey(id);
		if(lab.getLabIntroduce()!=null && !lab.getLabIntroduce().equals(""))
		   lab.setLabIntroduce(StringUtil.getInstace().zy(lab.getLabIntroduce()));
		modelMap.addAttribute("lab",lab);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatexlab","ViewTitle:修改实验室信息","smenu:lab","mmenu:updatelab"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param lab  需要更新实验室信息
	 * @return 成功的话则返回用于管理实验室的界面
	 */
	@RequestMapping("/updatexlabxx")
	public String updatexlabxx(ModelMap modelMap,Lab lab){
		
		//logger.info(lab.toString());
		int result = iLabService.updateByPrimaryKeyWithBLOBs(lab);
		if(result!=0){
			modelMap.addAttribute("update", true);
		}
		//logger.info(result);
		return "forward:/backend/labmgui";
	}
	/**
	 * 
	 * @param id  需要进行ajax删除的实验室的id
	 * @param modelMap
	 * @param request 用于获取跨域ajax需要返回的js函数的名称
	 * @return  ajax返回元页面，进行异步的刷新
	 */ 
	@RequestMapping("/delsys")
	@ResponseBody
	 public String delsys(@RequestParam("id") int id,ModelMap modelMap,HttpServletRequest request)
	 {
		 // logger.info(id);
		 int resultCode = iLabService.deleteByPrimaryKey(id);
		 //System.out.println(resultCode);
		 String callbackFunName = request.getParameter("callbackparam");
		 if(resultCode!=0)
		 {
			// modelMap.addAttribute("delFile", true);	 
			 return callbackFunName +"("+ "{'success':true}" +")";
		 }else{
			 //modelMap.addAttribute("delFile", false);
			 return callbackFunName +"("+ "{'success':false}" +")";
		 }
		 
		// return "forward:/backend/ZiliaoManeger/1";
	 }
	/**
	 * 
	 * @param modelMap
	 * @return  返回用于添加部门的界面
	 */
	@RequestMapping("/adddepartmentui")
	public String adddepartmentui(ModelMap modelMap){
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:adddepartment","ViewTitle:添加系部","smenu:department","mmenu:adddepartment"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param record  管理员输入的关于部门的信息
	 * @return 成功则返回用于管理部门的界面
	 */
	@RequestMapping("/adddepartment")
	public String adddepartment(ModelMap modelMap,Department record ){
		
		
		int result = departmentService.insert(record);
		if(result!=0){
			modelMap.addAttribute("message", true);
		}
	//	logger.info(result + "----result");
		return "forward:/backend/departmentmgui";
	}
	/**
	 * 
	 * @param modelMap
	 * @return  返回管理部门的界面
	 */
	@RequestMapping("/departmentmgui")
	public String departmentmgui(ModelMap modelMap){
		List<Department> departments = departmentService.selectalldepartment();
		modelMap.addAttribute("departments",departments);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:departmentmag","ViewTitle:系部管理","smenu:department","mmenu:mgdepartment"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param id 需要更新的部门id
	 * @param modelMap 
	 * @return 返回更新部门的界面
	 */
	@RequestMapping("/updatedepartments/{id}")
	public String updatedepartments(@PathVariable("id")int id,ModelMap modelMap){
		Department department = departmentService.selectByPrimaryKey(id);
		if(department.getdIntro()!=null && !department.getdIntro().equals(""))
			department.setdIntro(StringUtil.getInstace().zy(department.getdIntro()));
		modelMap.addAttribute("department",department);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatedepartment","ViewTitle:更新系部信息","smenu:department","mmenu:updatedepartment"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param major  前台传入的需要更新的部门信息
	 * @return 成功则返回部门管理界面
	 */
	@RequestMapping("/updatedepartmentxx")
	public String updatedepartmentxx(ModelMap modelMap,Department major){
		
		//logger.info(major.toString());
		int result = departmentService.updateByPrimaryKeyWithBLOBs(major);
		if(result!=0){
			modelMap.addAttribute("update", true);
		}
		//logger.info(result);
		return "forward:/backend/departmentmgui";
	}
	/**
	 * 
	 * @param id  需要进行ajax删除的部门的id
	 * @param modelMap
	 * @param request 用于获取跨域ajax需要返回的js函数的名称
	 * @return  ajax返回元页面，进行异步的刷新
	 */ 
	@RequestMapping("/deldepartment")
	@ResponseBody
	 public String deldepartment(@RequestParam("id") int id,ModelMap modelMap,HttpServletRequest request)
	 {
		 // logger.info(id);
		 int resultCode = departmentService.deleteByPrimaryKey(id);
		// System.out.println(resultCode);
		 String callbackFunName = request.getParameter("callbackparam");
		 if(resultCode!=0)
		 {
			// modelMap.addAttribute("delFile", true);	 
			 return callbackFunName +"("+ "{'success':true}" +")";
		 }else{
			 //modelMap.addAttribute("delFile", false);
			 return callbackFunName +"("+ "{'success':false}" +")";
		 }
		 
		// return "forward:/backend/ZiliaoManeger/1";
	 }

	/**
	 * 
	 * @param modelMap
	 * @return 但会添加学习原地的界面
	 */
	@RequestMapping("/addxxydtypeui")
	public String addxxydtypeui(ModelMap modelMap){
		
		//List<Teteacherytpe> ts = teteachertypeService.selectAllsteacher();
		//modelMap.addAttribute("ts",ts);
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addxxydtype","ViewTitle:添加学习园地类型","smenu:xxydtype","mmenu:addxxydtypeui"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param lab 学习原地类型的信息
	 * @param modelMap
	 * @return 返回管理学习原地类型的界面
	 */
	@RequestMapping("/addxxydtype")
	public String addxxydtype(Xxydtype lab,ModelMap modelMap ){
		
		xxydtypeService.insert(lab);
		return "forward:/backend/xxydtypemgui";
	}
	/**
	 * 
	 * @param modelMap
	 * @return  学习原地管理界面
	 */
	@RequestMapping("/xxydtypemgui")
	public String addxxydtypemgui(ModelMap modelMap)
	{  
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:xxydtypemag","ViewTitle:学习园地类型管理","smenu:xxydtype","mmenu:xxydtypemag"});				
		 
	    /*List<newsandbulletin> bulletins =newsandbulletService.selectAllBulletin();
		int newsesCount = newsandbulletService.selectAllNewsCount();
	    List<newsandbulletin> newses = newsandbulletService.selectAllNews();
	  
	  //  logger.info(bulletins.get(0).getId()+","+bulletins.get(0).getIndex());
		int bulletinsCount = newsandbulletService.selectAllBulletinCount();*/
		
		List<Xxydtype> xxydtypes = xxydtypeService.xxydtypes();
		modelMap.addAttribute("xxydtypes", xxydtypes);
		return PathUtil.returnStr(path, modelMap);
		
	}
	/**
	 * 
	 * @param id 更新的学习园地类型id
	 * @param modelMap
	 * @return 返回管理学习园地的界面
	 */
	@RequestMapping("/updatexxydtype/{id}")
	public String updatexxydtype(@PathVariable("id")int id,ModelMap modelMap){
		Xxydtype xxydtype = xxydtypeService.selectByPrimaryKey(id);
		modelMap.addAttribute("xxydtype",xxydtype);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatexxydtype","ViewTitle:更新学习园地类型","smenu:xxydtype","mmenu:updatexxydtype"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param major  更新学习原地类型
	 * @return 返回更新学习园地类型的界面
	 */
	@RequestMapping("/updatexxydtypexx")
	public String updatexxydtypexx(ModelMap modelMap,Xxydtype major){
		
		//logger.info(major.toString());
		int result = xxydtypeService.updateByPrimaryKey(major);
		if(result!=0){
			modelMap.addAttribute("update", true);
		}
		//logger.info(result);
		return "forward:/backend/xxydtypemgui";
	}
	/**
	 * 
	 * @param id  需要进行ajax删除的学习原地的id
	 * @param modelMap
	 * @param request 用于获取跨域ajax需要返回的js函数的名称
	 * @return  ajax返回元页面，进行异步的刷新
	 */ 
	@RequestMapping("/delxxydtype")
	@ResponseBody
	 public String delxxydtype(@RequestParam("id") int id,ModelMap modelMap,HttpServletRequest request)
	 {
		 // logger.info(id);
		 int resultCode = xxydtypeService.deleteByPrimaryKey(id);
		// System.out.println(resultCode);
		 String callbackFunName = request.getParameter("callbackparam");
		 if(resultCode!=0)
		 {
			// modelMap.addAttribute("delFile", true);	 
			 return callbackFunName +"("+ "{'success':true}" +")";
		 }else{
			 //modelMap.addAttribute("delFile", false);
			 return callbackFunName +"("+ "{'success':false}" +")";
		 }
		 
		// return "forward:/backend/ZiliaoManeger/1";
	 }
	/**
	 * 
	 * @param modelMap
	 * @return 添加学习园地的界面
	 */
	@RequestMapping("/addxxydui")
	public String addxxydui(ModelMap modelMap){
		
		//List<Teteacherytpe> ts = teteachertypeService.selectAllsteacher();
		//modelMap.addAttribute("ts",ts);
		List<Xxydtype> xxydtypes = xxydtypeService.xxydtypes();
		modelMap.addAttribute("xxydtypes", xxydtypes);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addxxyd","ViewTitle:添加学习园地信息","smenu:bxxyd","mmenu:addxxydui"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param lab 学习园地的基本信息 
	 * @param modelMap
	 * @param xxydtype 学习园地的类型
	 * @return 返回管理学习园地的界面
	 */
	@RequestMapping("/addxxyd")
	public String addxxyd(Xyyd lab,ModelMap modelMap,@RequestParam("xxydtype")int xxydtype){
		Xxydtype xxydtype2 = null;
		if(xxydtype!=0){
			 xxydtype2 = xxydtypeService.selectByPrimaryKey(xxydtype);
		}
		lab.setXyydtId(xxydtype2);
		xydService.insert(lab);
		return "forward:/backend/xxydmgui/1";
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页面
	 * @return 学习园地的管理界面
	 */
	@RequestMapping("/xxydmgui/{pagenow}")
	public String xxydmgui(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{  
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:xxydmag","ViewTitle:学习园地管理","smenu:bxxyd","mmenu:xxydmag"});				
		 
	    /*List<newsandbulletin> bulletins =newsandbulletService.selectAllBulletin();
		int newsesCount = newsandbulletService.selectAllNewsCount();
	    List<newsandbulletin> newses = newsandbulletService.selectAllNews();
	  
	  //  logger.info(bulletins.get(0).getId()+","+bulletins.get(0).getIndex());
		int bulletinsCount = newsandbulletService.selectAllBulletinCount();*/
		
		int pbulletincount=  xydService.countofxxyds();
		int pagesize = 10;
		int pagecount = getPageCount(pbulletincount, pagesize);
		List<Xyyd> xxyds = xydService.xxyds((pagenow-1)*pagesize, pagesize);
		
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("xxyds", xxyds);
		return PathUtil.returnStr(path, modelMap);
		
	}
	/**
	 * 
	 * @param allcount 整个list的大小
	 * @param pagesize 每页显示的数目
	 * @return 总共需要战士的页数大小
	 */
	public int getPageCount(int allcount,int pagesize)
	{
		//System.out.println("allcount---" + allcount + "--pagesize----");
		int pagecount = 0;
		if(allcount%pagesize == 0)
		{
			pagecount = allcount/pagesize;
		}else{
			pagecount = allcount/pagesize + 1;
		}
		return pagecount;
	}
	/**
	 * 
	 * @param id 需要更新的学习园地id
	 * @param modelMap
	 * @return 管理学习园地的界面
	 */
	@RequestMapping("/updatexxyd/{id}")
	public String updatexxyd(@PathVariable("id")int id,ModelMap modelMap){
		List<Xxydtype> xxydtypes = xxydtypeService.xxydtypes();
		modelMap.addAttribute("xxydtypes", xxydtypes);
		Xyyd xxyd = xydService.selectByPrimaryKey(id);
		String intro = xxyd.getXyydtIntro();
		if(intro!=null && !intro.equals(""))
			xxyd.setXyydtIntro(StringUtil.getInstace().zy(intro));
		modelMap.addAttribute("xxyd",xxyd);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatexxyd","ViewTitle:更新学习园地","smenu:bxxyd","mmenu:updatexxyd"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param major 学习园地信息
	 * @param xxydtype 学习园地类型
	 * @return 管理学习园地的界面
	 */
	@RequestMapping("/updatexxydxx")
	public String updatexxydxx(ModelMap modelMap,Xyyd major,@RequestParam("xxydtype")int xxydtype){
		Xxydtype xxydtype2 = null;
		if(xxydtype!=0){
			 xxydtype2 = xxydtypeService.selectByPrimaryKey(xxydtype);
		}
		major.setXyydtId(xxydtype2);
		
		int result = xydService.updateByPrimaryKeyWithBLOBs(major);
		if(result!=0){
			modelMap.addAttribute("update", true);
		}
		return "forward:/backend/xxydmgui/1";
	}
	/**
	 * 
	 * @param id  需要进行ajax删除的学习园地的id
	 * @param modelMap
	 * @param request 用于获取跨域ajax需要返回的js函数的名称
	 * @return  ajax返回元页面，进行异步的刷新
	 */ 
	@RequestMapping("/delxxyd")
	@ResponseBody
	 public String delxxyd(@RequestParam("id") int id,ModelMap modelMap,HttpServletRequest request)
	 {
		 // logger.info(id);
		 int resultCode = xydService.deleteByPrimaryKey(id);
		// System.out.println(resultCode);
		 String callbackFunName = request.getParameter("callbackparam");
		 if(resultCode!=0)
		 {
			// modelMap.addAttribute("delFile", true);	 
			 return callbackFunName +"("+ "{'success':true}" +")";
		 }else{
			 //modelMap.addAttribute("delFile", false);
			 return callbackFunName +"("+ "{'success':false}" +")";
		 }
		 
		// return "forward:/backend/ZiliaoManeger/1";
	 }
	/**
	 * 
	 * @param modelMap
	 * @return 添加图片的界面
	 */
	@RequestMapping("/addimageui")
	public String addimageui(ModelMap modelMap){
		
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addimage","ViewTitle:添加专项图片","smenu:image","mmenu:addimage"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param files  图片对象
	 * @param indexImage 图片属性对象
	 * @return 管理图片界面
	 */
	@RequestMapping("/addimage")
	public String addimage(ModelMap modelMap,@RequestParam(value = "fileste", 
			required = false) MultipartFile [] files,IndexImage indexImage){
		String imagePaths=uploadFiels(files);
		indexImage.setImgUrl(imagePaths);
		int resultCode = indexImageService.insert(indexImage);
		if(resultCode!=0){
			modelMap.addAttribute("insertsuccess", true);
		}
		else{
			modelMap.addAttribute("insertsuccess", false);
		}
		return "forward:/backend/imagemag";
	}
	/**
	 * 
	 * @param modelMap
	 * @param id  需要更新的图片地址is
	 * @return 管理图片的界面
	 */
	@RequestMapping("/updateimageui/{id}")
	public String updateimageui(ModelMap modelMap,@PathVariable("id")int id){
		IndexImage indexImage = indexImageService.selectByPrimaryKey(id);
		modelMap.addAttribute("indexImage", indexImage);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updateimage","ViewTitle:修改专项图片","smenu:image","mmenu:updateimage"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param files 上传的图片对象
	 * @param image 图片的基本属性
	 * @return 返回图片的管理界面
	 */
	@RequestMapping("/updateimage")
	public String updateimage(ModelMap modelMap,@RequestParam(value = "fileste", 
			required = false) MultipartFile [] files,IndexImage image){
		String imagePaths=uploadFiels(files);
		IndexImage indexImage = new IndexImage();
		//logger.info(imagePaths);
		if(!imagePaths.equals("") && imagePaths.length()!=0)
		{
			//logger.info("----test");
			indexImage.setImgUrl(imagePaths);
		}else{
			
			indexImage = indexImageService.selectByPrimaryKey(image.getImgId());
			// logger.info(teacher1.toString());
			image.setImgUrl(indexImage.getImgUrl());
		}
		int resultCoude = indexImageService.updateByPrimaryKey(image);
		if(resultCoude!=0)
			modelMap.addAttribute("success",true);
		return "forward:/backend/imagemag";
	}
	@RequestMapping("/imagemag")
	public String imagemag(ModelMap modelMap){
		List<IndexImage> images = indexImageService.images();
		modelMap.addAttribute("images", images);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:imagemag","ViewTitle:管理专项图片","smenu:image","mmenu:imagemag"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param files 要进行上传的图片文件
	 * @return 上传照片的路径
	 */
		public String uploadFiels(MultipartFile[] files) {
			String imagepath = "";
			for (MultipartFile file : files) {
				if (file.isEmpty())
					continue;
				else {
					imagepath = com.mc.utils.UploadUtil.getInstance().upload(
							file,
							StringUtil2.getInstance()
									.getPhysicsPath("/images/books"));
					
				}
			}
			return imagepath;
		}
		@RequestMapping("/delindeximage")
		@ResponseBody
		 public String delindeximage(@RequestParam("id") int id,ModelMap modelMap,HttpServletRequest request)
		 {
			 int resultCode =indexImageService.deleteByPrimaryKey(id);
			 String callbackFunName = request.getParameter("callbackparam");
			 if(resultCode!=0)
			 {
				// modelMap.addAttribute("delFile", true);	 
				 return callbackFunName +"("+ "{'success':true}" +")";
			 }else{
				 //modelMap.addAttribute("delFile", false);
				 return callbackFunName +"("+ "{'success':false}" +")";
			 }
		 }

}
